﻿using System;
using System.Data.Entity;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZSZ.DTO;
using ZSZ.IService;
using ZSZ.Model;

namespace ZSZ.Service
{
    public partial class AdminUserPermissionService : BaseService<AdminUserPermission>, IAdminUserPermissionService
    {
        private IQueryable<AdminUserPermission> GetJoinAll()
        {
            return GetAll()
                .Include(ap=>ap.Permission)
                .AsNoTracking();
        }
        public AdminUserPermissionDTO[] GetHasPermissions(long adminId)
        {
            return GetAll().Where(ap=>ap.AdminUserId== adminId).ToList().Select(ap=>ToDTO(ap)).ToArray();
        }

        public AdminUserPermission GetSingle(long adminId, long permissionId)
        {
           IQueryable<AdminUserPermission> entity = GetAll().Where(ap => ap.AdminUserId == adminId && ap.PermissionId == permissionId);
            int count = entity.Count();
            if (count<=0)
            {
                return null;
            }
            else if (count==1)
            {
                return entity.Single();
            }
            else
            {
                throw new Exception("获取到多条管理员(Id:" + adminId + ")和权限(Id:" + permissionId + ")关联信息");
            }
        }

        public AdminUserPermissionDTO GetAdminUserPermission(long adminId, string controllerName, string actionName, string httpMethod)
        {
            IQueryable<AdminUserPermission> entity = GetJoinAll().Where(ap => ap.AdminUserId == adminId 
            && ap.Permission.ControllerName == controllerName
            && ap.Permission.ActionName == actionName
            && ap.Permission.HttpMethod.Contains(httpMethod));
            int count = entity.Count();
            if (count <= 0)
            {
                return null;
            }
            else if (count == 1)
            {
                return ToDTO(entity.Single());
            }
            else
            {
                throw new Exception("获取到多条管理员(Id:" + adminId + ")和权限(Id:" + entity.Select(ap=>ap.PermissionId) + ")关联信息");
            }
        }

        private AdminUserPermissionDTO ToDTO(AdminUserPermission entity)
        {
            AdminUserPermissionDTO dto = new AdminUserPermissionDTO();
            dto.CreateDateTime = entity.CreateDateTime;
            dto.Id = entity.Id;
            dto.AdminUserId = entity.AdminUserId;
            dto.PermissionId = entity.PermissionId;
            dto.HasPermission = entity.HasPermission;
            return dto;
        }

        public void Add(long adminId, long permissionId, bool value)
        {
            AdminUserPermission entity = new AdminUserPermission();
            entity.AdminUserId = adminId;
            entity.PermissionId = permissionId;
            entity.HasPermission = value;
            Add(entity);
        }

        public void Update(long adminId, long permissionId, bool value)
        {
            AdminUserPermission entity = GetSingle( adminId,  permissionId);
            if (entity==null)
            {
                throw new Exception("获取不到管理员(Id:"+ adminId + ")和权限(Id:"+ permissionId + ")关联信息");
            }
            entity.HasPermission = value;
            Update(entity);
        }
        public void Deleted(long adminId, long permissionId)
        {
            AdminUserPermission entity = GetSingle( adminId,  permissionId);
            if (entity==null)
            {
                throw new Exception("获取不到管理员(Id:"+ adminId + ")和权限(Id:"+ permissionId + ")关联信息");
            }
            Deleted(entity);
        }

    }
}
